package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.ui.home.activity.CoachIdleTimeActivity;
import com.hongyuan.sellfitness.ui.home.activity.CoachLessonActivity;
import com.hongyuan.sellfitness.ui.home.activity.CoachProfileActivity;
import com.hongyuan.sellfitness.ui.home.beans.CoachDeatilsBean;
import com.hongyuan.sellfitness.ui.home.beans.CoachIdleTimeBean;
import com.hongyuan.sellfitness.ui.home.beans.CoachLessonItemBean;
import com.hongyuan.sellfitness.ui.main.activity.CoachImgActivity;
import com.hongyuan.sellfitness.ui.main.beans.AuthorityBean;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CoachDetailsViewModel extends CustomViewModel<MyRepository> {

    //权限管理
    public ObservableField<AuthorityBean> authority = new ObservableField<>();
    public ObservableField<CoachDeatilsBean> entity = new ObservableField<>();
    public ObservableField<CoachIdleTimeBean> timeEntity = new ObservableField<>();

    public CoachDetailsViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //个人介绍
    public BindingCommand goProfile = new BindingCommand(() ->{
        Bundle bundle = new Bundle();
        bundle.putSerializable("profiles",entity.get());
        startActivity(CoachProfileActivity.class,bundle);
    });
    //更多私教课
    public BindingCommand moreClass = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("coach_mid",String.valueOf(entity.get().getInfo().getM_id()));
        startActivity(CoachLessonActivity.class,bundle);
    });

    //空闲时间
    public BindingCommand goIdleTime = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("coach_mid",String.valueOf(entity.get().getInfo().getM_id()));
        startActivity(CoachIdleTimeActivity.class,bundle);
    });

    //联系教练
    public BindingCommand callTel = new BindingCommand(() -> getUC().getCallTel().setValue(entity.get().getInfo().getM_mobile()));


    //给RecyclerView添加ObservableList
    public ObservableList<CoachLessonItemViewModel> lessonList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CoachLessonItemViewModel> lessonBinding = ItemBinding.of(BR.viewModel, R.layout.item_coach_lesson);

    //教练风采
    public BindingCommand goCoachStyleImg = new BindingCommand(() -> {
        if(BaseUtil.isValue(entity.get().getC_photo1().getImg_src())){
            Bundle bundle = new Bundle();
            bundle.putString("title","教练风采");
            bundle.putString("coach_mid",String.valueOf(entity.get().getInfo().getM_id()));
            bundle.putString("photo_category_id","1");
            startActivity(CoachImgActivity.class,bundle);
        }else{
            ToastUtils.showShort("暂无内容");
        }

    });
    //教练成功案例
    public BindingCommand goCoachSuccessImg = new BindingCommand(() -> {
        if(BaseUtil.isValue(entity.get().getC_photo1().getImg_src())){
            Bundle bundle = new Bundle();
            bundle.putString("title","成功案例");
            bundle.putString("coach_mid",String.valueOf(entity.get().getInfo().getM_id()));
            bundle.putString("photo_category_id","2");
            startActivity(CoachImgActivity.class,bundle);
        }else{
            ToastUtils.showShort("暂无内容");
        }
    });
    //专业证书
    public BindingCommand goCoachCertificateImg = new BindingCommand(() -> {
        if(BaseUtil.isValue(entity.get().getC_photo1().getImg_src())){
            Bundle bundle = new Bundle();
            bundle.putString("title","专业证书");
            bundle.putString("coach_mid",String.valueOf(entity.get().getInfo().getM_id()));
            bundle.putString("photo_category_id","3");
            startActivity(CoachImgActivity.class,bundle);
        }else{
            ToastUtils.showShort("暂无内容");
        }

    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.VERIFY_ROLE){
            AuthorityBean bean = (AuthorityBean)dataBean;
            //设定死的，权限功能服务端还有问题
            bean.setState(1);
            authority.set(bean);
        }

        if(code == Constants.GET_COACH_LAST_KONG_TIME){
            CoachIdleTimeBean idleTimeBean = (CoachIdleTimeBean)dataBean;
            timeEntity.set(idleTimeBean);
        }

        if(code == Constants.GET_COACH_INDEX_INFO){
            CoachDeatilsBean bean = (CoachDeatilsBean)dataBean;
            entity.set(bean);
        }

        if(code == Constants.GET_COACH_COURSE_PRIVITE){
            List<CoachLessonItemBean.ListBean> list = ((CoachLessonItemBean)dataBean).getList();
            if(list != null && list.size() > 0){
                for(int i = 0 ; i < list.size() ; i++){
                    CoachLessonItemViewModel itemViewModel = new CoachLessonItemViewModel(this,list.get(i),i,list.size() - 1);
                    lessonList.add(itemViewModel);
                }
            }
        }
    }
}
